// 本地存储服务
export class StorageService {
  private static getItem<T>(key: string, defaultValue: T): T {
    try {
      const item = localStorage.getItem(key)
      return item ? JSON.parse(item) : defaultValue
    } catch {
      return defaultValue
    }
  }

  private static setItem<T>(key: string, value: T): void {
    try {
      localStorage.setItem(key, JSON.stringify(value))
    } catch (error) {
      console.error('Failed to save to localStorage:', error)
    }
  }

  // 生活小确幸相关
  static getSweetMoments() {
    return this.getItem('sweetMoments', [])
  }

  static saveSweetMoments(moments: any[]) {
    this.setItem('sweetMoments', moments)
  }

  // 情书相关
  static getLoveLetters() {
    return this.getItem('loveLetters', [])
  }

  static saveLoveLetters(letters: any[]) {
    this.setItem('loveLetters', letters)
  }

  // 便签相关
  static getStickyNotes() {
    return this.getItem('stickyNotes', [])
  }

  static saveStickyNotes(notes: any[]) {
    this.setItem('stickyNotes', notes)
  }

  // 愿望清单相关
  static getBucketList() {
    return this.getItem('bucketList', [])
  }

  static saveBucketList(items: any[]) {
    this.setItem('bucketList', items)
  }

  // 故事相关
  static getOurStory() {
    return this.getItem('ourStory', {
      meetingStory: { his: '', hers: '' },
      heartMoment: '',
      timeline: []
    })
  }

  static saveOurStory(story: any) {
    this.setItem('ourStory', story)
  }

  // 音乐播放列表
  static getMusicPlaylist() {
    return this.getItem('musicPlaylist', [])
  }

  static saveMusicPlaylist(playlist: any[]) {
    this.setItem('musicPlaylist', playlist)
  }

  // 设置相关
  static getSettings() {
    return this.getItem('blogSettings', {
      theme: 'default',
      autoPlay: false,
      notifications: true
    })
  }

  static saveSettings(settings: any) {
    this.setItem('blogSettings', settings)
  }
}

// 生成唯一ID
export const generateId = () => {
  return Date.now().toString(36) + Math.random().toString(36).substr(2)
}

// 格式化日期
export const formatDate = (date: Date = new Date()) => {
  return date.toISOString().split('T')[0]
}

// 格式化时间
export const formatTime = (date: Date = new Date()) => {
  return date.toLocaleTimeString('zh-CN', { 
    hour: '2-digit', 
    minute: '2-digit',
    hour12: false 
  })
}
